<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="dataForm.age" controls-position="right" :min="0" :max="100" label="年龄"></el-input-number>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="introduction">
        <ke-editor v-model="dataForm.introduction"></ke-editor>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="repeatVisible">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>


  export default {
    data() {
      return {
        visible: false,
        repeatVisible: false,
        dataForm: {
          id: 0,
          name: '',
          age: '',
          address: '',
          introduction: ''
        },
        dataRule: {
          name: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          age: [
            {required: true, message: '年龄不能为空', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '地址不能为空', trigger: 'blur'}
          ],
          introduction: [
            {required: true, message: '简介不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      onContentChange (val) {
        this.dataForm.introduction = val
      },
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            // 获取该用户信息
            axios.get('/user/info/' + this.dataForm.id).then(data => {
              if (data.data.code === 0){
                this.dataForm.name = data.data.user.name
                this.dataForm.age = data.data.user.age
                this.dataForm.address = data.data.user.address
                this.dataForm.introduction = data.data.user.introduction
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.dataForm.name = this.dataForm.name.replace(/(^\s*)|(\s*$)/g, '')
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.repeatVisible = true
            let params = {
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'age': this.dataForm.age,
              'address': this.dataForm.address,
              'introduction': this.dataForm.introduction
            }
            let req = !this.dataForm.id ? this.$axios({method:"post",url:"/user/save",data: params}) : this.$axios({method:"post",url:"/user/update",data: params});
            req.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                    setTimeout(() => {
                      this.repeatVisible = false
                    }, 2000)
                  }
                })
              } else {
                this.$message.error(data.msg)
                setTimeout(() => {
                  this.repeatVisible = false
                }, 2000)
              }
            })
          }
        })
      }
    }
  }
</script>
